package online.sixfish.question.controller;


import online.sixfish.common.api.ApiResult;
import online.sixfish.judger.pojo.JudgeProgramReq;
import online.sixfish.judger.pojo.TestResult;
import online.sixfish.question.model.entity.MockMatchRecord;
import online.sixfish.question.model.vo.MockMatchReq;
import online.sixfish.question.model.vo.QueProgramVO;
import online.sixfish.question.service.JudgeService;
import online.sixfish.question.service.impl.MockMatchService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
@RequestMapping("/mock-match")
public class MockMatchController {
    @Resource
    MockMatchService mockMatchService;
    @Resource
    JudgeService judgeService;

    @GetMapping("/{id}")
    @PreAuthorize("hasRole('user')")
    public ApiResult findById(@PathVariable Integer id){
        MockMatchRecord mockMatch = mockMatchService.findById(id);
        return ApiResult.success(mockMatch);
    }


    @PostMapping("/start")
    @PreAuthorize("hasRole('user')")
    public ApiResult start(@RequestBody MockMatchReq mockMatchReq){
        Integer matchId = mockMatchService.currentMatchId();
        if(Objects.nonNull(matchId)){
            return ApiResult.success(matchId,"当前模拟赛场还未结束。");
        }
        Integer id = mockMatchService.start(mockMatchReq);
        return ApiResult.success(id);
    }

    @PostMapping("/question-list/{id}")
    @PreAuthorize("hasRole('user')")
    public ApiResult questionList(@PathVariable Integer id){
        List<QueProgramVO> queProgramVOS = mockMatchService.questionList(id);
        return ApiResult.success(queProgramVOS);
    }


    @PostMapping("/judge/program")
    @PreAuthorize("hasRole('user')")
    public ApiResult judgeQueProgram(@RequestBody JudgeProgramReq judgeProgramReq
    ,@RequestParam String matchId) {
        judgeService.judgeMatchProgram(judgeProgramReq,matchId);
        TestResult result = judgeService.judgeProgram(judgeProgramReq);

        return ApiResult.success(result);

    }

}
